﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;
using WinFormsAppTest.Dao;
using WinFormsAppTest.pojo;
using WinFormsAppTest.Service;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace WinFormsAppTest
{
    public partial class AdminManageForm : Form
    {
        public String adminname = "null";//设置默认登录用户(手动添加)
        public int id = 0;//设置默认登录用户(手动添加)

        public AdminManageForm()
        {
            InitializeComponent();
        }


        // -------有参 窗口（手动添加）--------
        public AdminManageForm(String name, int uid)
        {
            adminname = name;
            id = uid;
            InitializeComponent();
        }
        //-----------------------------------

        private void AdminManageForm_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                // 关闭进程
                Process.GetCurrentProcess().Kill();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timelabel.Text = DateTime.Now.ToString("G");
        }

        private void AdminManageForm_Load(object sender, EventArgs e)
        {
            //调整时间控件
            timer1.Interval = 1000;
            timer1.Start();
            //

            //开始添加表格中的数据
            AdminService adminservice = new AdminService();

            List<WinFormsAppTest.pojo.Admin> users = new List<WinFormsAppTest.pojo.Admin>();
            users = adminservice.GetAdmins();

            int _i = users.Count - 1;
            if (users.Count == 0)
            {
                MessageBox.Show("管理员信息信息无法查询!");
            }
            //升序版
            for (int j = 0; j < users.Count; j++)
            {
                pojo.Admin f = users[j];
                int myNewRowIndex = UserdataGridView.Rows.Add();
                UserdataGridView.Rows[myNewRowIndex].Cells["AdminIdColumn"].Value = f.Adminid;
                UserdataGridView.Rows[myNewRowIndex].Cells["AdminNameColumn"].Value = f.Name;
                UserdataGridView.Rows[myNewRowIndex].Cells["PasswordColumn"].Value = f.Password;
                UserdataGridView.Rows[myNewRowIndex].Cells["UpdateColumn"].Value = "更新";
                UserdataGridView.Rows[myNewRowIndex].Cells["DeleteColumn"].Value = "删除";
            }
            //
        }

        private void Exitbutton_Click(object sender, EventArgs e)
        {
            this.Hide();
            AdminForm f = new AdminForm(adminname, id);
            f.ShowDialog();
            this.Dispose();
        }

        private void UserdataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //判断单元格类型是否为按钮
            if (UserdataGridView.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
            {
                //获取按钮所在列的名字
                string columnName = UserdataGridView.Columns[e.ColumnIndex].Name;
                //获取按钮的一整行
                DataGridViewRow row = UserdataGridView.Rows[e.RowIndex];
                //由于row.Cells["IntColumnName"].Value得到的取值为object型 需要用该方法进行转换
                int userid = Convert.ToInt32(row.Cells["AdminIdColumn"].Value);
                string name = Convert.ToString(row.Cells["AdminNameColumn"].Value);
                string password = Convert.ToString(row.Cells["PasswordColumn"].Value);
                if (columnName == "UpdateColumn")
                {
                    MessageBox.Show("更新" + userid + " " + name + " " + password);
                    this.Hide();
                    AdminUpdateForm f = new AdminUpdateForm(userid, name, password, adminname, id);
                    f.ShowDialog();
                    this.Dispose();

                }
                else if (columnName == "DeleteColumn")
                {
                    //MessageBox.Show("删除" + userid + " " + name + " " + password);
                    AdminDao admindao = new AdminDao();
                    admindao.DeleteAdminById(userid);

                    //重新加载一遍界面，达到刷新的效果
                    this.Hide();
                    AdminManageForm f = new AdminManageForm(adminname, id);
                    //保证显示的位置和上个界面相同
                    f.Location = this.Location;
                    f.ShowDialog();
                    this.Dispose();
                }
            }
        }

        private void Addbutton_Click(object sender, EventArgs e)
        {
            this.Hide();
            AdminAddForm f = new AdminAddForm(adminname, id);
            f.ShowDialog();
            this.Dispose();
        }

        private void searchbutton_Click(object sender, EventArgs e)
        {

            if (string.IsNullOrWhiteSpace(searchtextBox.Text)
                || searchcomboBox.SelectedItem == null)//判断复选框是否选中
            {
                UserdataGridView.Rows.Clear();
                DataGridViewClass dataGridView = new DataGridViewClass();
                UserdataGridView = dataGridView.GetAdminsData(UserdataGridView);
            }
            else
            {
                string combox = searchcomboBox.SelectedItem.ToString();
                UserdataGridView.Rows.Clear();
                if (int.TryParse(searchtextBox.Text, out int number)
                    && combox == "管理员id")
                {
                    if (number != 0 && searchtextBox.Text != "请输入搜索内容")
                    {
                        DataGridViewClass dataGridView = new DataGridViewClass();
                        UserdataGridView = dataGridView.GetAdminDataById(UserdataGridView, number);
                    }
                    else
                    {
                        DataGridViewClass dataGridView = new DataGridViewClass();
                        UserdataGridView = dataGridView.GetAdminsData(UserdataGridView);
                    }

                }
                else if (combox == "管理员名")
                {
                    if (searchtextBox.Text != "" && searchtextBox.Text != "请输入搜索内容")
                    {
                        DataGridViewClass dataGridView = new DataGridViewClass();
                        UserdataGridView = dataGridView.GetAdminDataByName(UserdataGridView, searchtextBox.Text);
                    }
                    else
                    {
                        DataGridViewClass dataGridView = new DataGridViewClass();
                        UserdataGridView = dataGridView.GetAdminsData(UserdataGridView);
                    }

                }
                else
                {
                    DataGridViewClass dataGridView = new DataGridViewClass();
                    UserdataGridView = dataGridView.GetAdminsData(UserdataGridView);
                }
            }
        }

        private void searchtextBox_enter(object sender, EventArgs e)
        {
            if (searchtextBox.Text == "请输入搜索内容")
                searchtextBox.Text = "";
            searchtextBox.ForeColor = Color.Black;
        }

        private void searchtextBox_leave(object sender, EventArgs e)
        {
            if (searchtextBox.Text == "")
            {
                searchtextBox.Text = "请输入搜索内容";
                searchtextBox.ForeColor = Color.Gray;
            }
        }
    }
}
